<template>
  <div>
    <el-form
      :inline="true"
      :model="searchMap"
      ref="searchMap"
      class="demo-form-inline"
    >
      <el-form-item prop="username">
        <el-input v-model="searchMap.username" placeholder="账户"></el-input>
      </el-form-item>
      <el-form-item prop="name">
        <el-input v-model="searchMap.name" placeholder="用户名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="fencthData">查询</el-button>
        <el-button type="primary" @click="handleAddMember('addMemeberForm')"
          >新增</el-button
        >
        <el-button @click="resetForm('searchMap')">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="list" height="550" border style="width: 100%">
      <el-table-column type="index" label="序号"></el-table-column>
      <el-table-column prop="username" label="账户"></el-table-column>
      <el-table-column prop="name" label="用户名"></el-table-column>
      <el-table-column prop="age" label="年龄"></el-table-column>
      <el-table-column prop="mobile" label="联系电话"></el-table-column>
      <el-table-column prop="salary" label="薪酬"></el-table-column>
      <el-table-column prop="entryDate" label="入职时间"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.row.id, 'addMemeberForm')"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>

    <el-dialog
      :title="formTitle"
      :visible.sync="dialogFormVisible"
      width="500px"
    >
      <el-form
        ref="addMemeberForm"
        label-width="100px"
        label-position="right"
        style="width:400px"
        :rules="rules"
        :model="newMemberInfo"
      >
        <el-form-item label="账户" prop="username">
          <el-input v-model="newMemberInfo.username"></el-input>
        </el-form-item>
        <el-form-item label="用户名" prop="name">
          <el-input v-model="newMemberInfo.name"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="newMemberInfo.age"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="mobile">
          <el-input v-model="newMemberInfo.mobile"></el-input>
        </el-form-item>
        <el-form-item label="薪酬" prop="salary">
          <el-input v-model="newMemberInfo.salary"></el-input>
        </el-form-item>
        <el-form-item label="入职时间" prop="entryDate">
          <el-date-picker
            value-format="yyyy-MM-dd"
            v-model="newMemberInfo.entryDate"
            type="date"
            placeholder="出生日期"
            prop="birthday"
          ></el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancleForm('addMemeberForm')">取 消</el-button>
        <el-button
          type="primary"
          @click="
            newMemberInfo.id === ''
              ? addMember('addMemeberForm')
              : updateMember('addMemeberForm')
          "
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script>
import OpApi from "@/api/baseSCUD";
const resource = "staffs";
const cresource = "员工";

export default {
  data() {
    return {
      list: [],
      total: 0,
      currentPage: 1,
      pageSize: 10,
      formTitle: "新增" + cresource,
      searchMap: {
        username: "",
        name: ""
      },
      dialogFormVisible: false,
      newMemberInfo: {
        id: "",
        username: "",
        name: "",
        age: "",
        mobile: "",
        salary: "",
        entryDate: ""
      },
      rules: {
        name: [
          {
            required: true,
            message: "请输入" + cresource + "姓名",
            trigger: "blur"
          }
        ],
        username: [{ required: true, message: "请输入账户", trigger: "blur" }]
      }
    };
  },
  methods: {
    fencthData() {
      OpApi.search(
        resource,
        this.currentPage,
        this.pageSize,
        this.searchMap
      ).then(response => {
        console.log(response.data);
        const respData = response.data;
        if (respData.flag) {
          this.total = respData.data.total;
          this.list = respData.data.rows;
          console.log(this.list);
        } else {
          this.$message({
            message: response.data.message,
            type: "warning",
            duration: 1000
          });
        }
      });
    },
    handleEdit(id, formName) {
      this.dialogFormVisible = true;
      this.formTitle = "" + cresource + "编辑";
      this.$nextTick(() => {
        this.$refs[formName].resetFields();
      });
      OpApi.getById(resource, id).then(response => {
        if (response.data.flag) {
          this.newMemberInfo = response.data.data;
          console.log(this.newMemberInfo);
        } else {
          this.$message({
            message: response.data.message,
            type: "warning",
            duration: 1000
          });
        }
      });
    },
    handleDelete(id) {
      console.log(id);
      this.$confirm("此操作将删除该" + cresource + "信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          OpApi.delById(resource, id).then(response => {
            if (response.data.flag) {
              this.fencthData();
              this.$message({
                type: "success",
                message: "删除成功!"
              });
            } else {
              this.$message({
                message: response.data.message,
                type: "warning",
                duration: 1000
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.fencthData();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
      this.fencthData();
    },
    resetForm(formName) {
      console.log("resetForm:", formName);
      this.$refs[formName].resetFields();
    },
    cancleForm(formName) {
      console.log("cancleForm:", formName);
      this.$refs[formName].resetFields();
      this.dialogFormVisible = false;
    },
    handleAddMember(formName) {
      this.dialogFormVisible = true;
      this.formTitle = "新增" + cresource + "";
      console.log("this.newMemberInfo.id", this.newMemberInfo.id);
      console.log("this.newMemberInfo.id", this.newMemberInfo.id === "");
      console.log("this.newMemberInfo.id", this.newMemberInfo.id === null);
      this.$nextTick(() => {
        this.$refs[formName].resetFields();
      });
      // this.$refs[formName].resetFields();
    },
    addMember(formName) {
      this.$refs[formName].validate(valid => {
        if (!valid) {
          return;
        } else {
          console.log("addMember resetForm:", formName);
          OpApi.add(resource, this.newMemberInfo).then(response => {
            console.log(response.data);
            const respData = response.data;
            if (respData.flag) {
              this.$message({
                message: "添加" + cresource + "Success",
                type: "success",
                duration: 1000
              });
              this.dialogFormVisible = false;
              this.$refs[formName].resetFields();
              this.fencthData();
            } else {
              this.$message({
                message: response.data.message,
                type: "warning",
                duration: 1000
              });
            }
          });
        }
      });

      // this.$refs[formName].resetFields();
    },
    updateMember(formName) {
      this.$refs[formName].validate(valid => {
        if (!valid) {
          return;
        } else {
          console.log("updateMember resetForm:", formName);
          OpApi.update(resource, this.newMemberInfo).then(response => {
            console.log(response.data);
            const respData = response.data;
            if (respData.flag) {
              this.$message({
                message: "" + cresource + "更新Success",
                type: "success",
                duration: 1000
              });
              this.dialogFormVisible = false;
              this.$refs[formName].resetFields();
              this.fencthData();
            } else {
              this.$message({
                message: response.data.message,
                type: "warning",
                duration: 1000
              });
            }
          });
        }
      });

      // this.$refs[formName].resetFields();
    }
  },
  created() {
    this.fencthData();
  }
};
</script>
<style scoped>
.el-form {
  margin-top: 20px;
}
</style>
